package design_pattern.CountDownMode;

/**
 * @Author: 蔡翔
 * @Date: 2019/11/17 13:19
 * @Version 1.0
 */
public class CountDown {
    private final int total;
    private int counter;
    public CountDown(Integer total){
        this.total = total;
    }
    public void down(){
        synchronized (this){
            counter++;
            // 当一个线程执行玩了，结束任务以后就会唤醒全部正在等待的线程，让他们去尝试判断一下，是否counter!=total
            notifyAll();
        }
    }

    public void await() throws InterruptedException{
        synchronized (this){
            while (counter!=total){
                this.wait();
            }
        }
    }

}
